# !/usr/bin/env python
# -*-coding:utf-8 -*-

"""
# File       : openmaxfileno.py
# Time       ：2024/5/8 15:27
# Author     ：huchl
# version    ：python 3.6
# Description：最大文件打开数
"""
import sys


def test_file_descriptors():
    max_fd_attempts = 10000  # 尝试打开的最大文件描述符数量，可以根据需要调整
    current_fd = 0

    try:
        while True:
            with open(f'file{current_fd}', 'w') as f:
                print(f"open {current_fd + 1} file descriptors")
                current_fd += 1

                # 如果达到最大尝试数，则退出循环
                if current_fd >= max_fd_attempts:
                    print(f"Reached maximum attempts of {max_fd_attempts} file descriptors.")
                    break
    except OSError as e:
        # 当打开文件失败时，打印错误信息并退出
        print(f"Error occurred while opening file: {e}")
        print(f"Maximum number of open file descriptors is around {current_fd}.")
        sys.exit(1)


if __name__ == "__main__":
    test_file_descriptors()
